//
//  vdTextfield.m
//  Void Dead
//
//  Created by Sidney Just on 13.03.10.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdTextField.h"

@implementation vdTextField
@synthesize textAlignment, keyboardType, hideOnLostFocus, hideOnReturn;
@synthesize offsetLeft, offsetRight, isKeyboardVisible, text, delegate, maxCharacters;

- (void)endSequence:(CGPoint)point {
	if(!(flags & SHOW))
		return;
	
	if(point.x >= background.aPos_x && point.x <= background.aPos_x + background.size_x && point.y >= background.aPos_y && point.y <= background.aPos_y + background.size_y)
	{
		[self showKeyboard];
	} else {
		
		if(hideOnLostFocus)
			[self hideKeyboard];
	}

}

- (BOOL)hideKeyboard {
	BOOL temp = [textfield resignFirstResponder];
	
	if(temp)
	{
		isKeyboardVisible = NO;
	}
	
	return temp;
}

- (BOOL)showKeyboard {
	BOOL temp = [textfield becomeFirstResponder];
	
	if(temp)
	{
		isKeyboardVisible = YES;
	}
	
	return temp;
}

- (void)setTexture:(NSString *)_tex {
	[[vdTexManager sharedManager] deleteTextureNode:background.texture];
	
	[background setTexture:[[vdTexManager sharedManager] loadTexture:_tex]];
	
	if(_tex)
	{
		label.pos_y = ((background.size_y / 2) - (label.size_y / 2));
		
		if(textAlignment == UITextAlignmentLeft)
			label.pos_x = (float)offsetLeft;
		
		if(textAlignment == UITextAlignmentRight)
			label.pos_x = background.size_x - label.size_x - (float)offsetRight;
		
		if(textAlignment == UITextAlignmentCenter)
			label.pos_x = ((background.size_x / 2) - (label.size_x / 2)) + (float)offsetLeft - (float)offsetRight;
	}
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	if(hideOnReturn)
	{
		[self hideKeyboard];
	}
	
	return NO;
}

- (void)changedCharacter {
	if(maxCharacters > 0)
	{
		if([textfield.text length] > maxCharacters)
		{
			[textfield setText:label.string];
		}
	}
	
	[label setString:textfield.text];
	
	if(textAlignment == UITextAlignmentLeft)
		label.pos_x = (float)offsetLeft;
	
	if(textAlignment == UITextAlignmentRight)
		label.pos_x = background.size_x - label.size_x - (float)offsetLeft;
	
	if(textAlignment == UITextAlignmentCenter)
		label.pos_x = ((background.size_x / 2) - (label.size_x / 2)) + (float)offsetLeft - (float)offsetRight;
	
	text = label.string;
	
	if(delegate)
	{
		if([delegate conformsToProtocol:@protocol(vdInterfaceProtocol)])
		{
			if([delegate respondsToSelector:@selector(textFieldChangedCharacter:)])
				[delegate textFieldChangedCharacter:self];
		}
	}
}

- (void)setText:(NSString *)string {
	[textfield setText:string];
	[self changedCharacter];
}

- (void)setKeyboardType:(UIKeyboardType)type {
	[textfield setKeyboardType:type];
}

- (id)init {
	if(self	= [super init])
	{
		textfield = [[UITextField alloc] initWithFrame:CGRectMake(-128, -32, 10, 32)];
		[textfield addTarget:self action:@selector(changedCharacter) forControlEvents:UIControlEventEditingChanged];
		[textfield setDelegate:self];
		textfield.autocorrectionType = UITextAutocorrectionTypeNo;
		
		keyboardType = UIKeyboardTypeDefault;
		textAlignment = UITextAlignmentLeft;
		
		[[vdKernel sharedKernel].renderView addSubview:textfield];
		[textfield setHidden:YES];
		
		isKeyboardVisible = NO;
		
		background = [[vdShape alloc] initWithTexture:NULL];
		[self addChild:background];
		
		offsetLeft = 5;
		offsetRight = 5;
		
		maxCharacters = 0;
		
		label = [vdLabel labelWithText:@" "];
		label.color->red = 0.0f;
		label.color->green = 0.0f;
		label.color->blue = 0.0f;
		[background addChild:label];
		 
		hideOnLostFocus = YES;
		hideOnReturn = YES;
		
		delegate = NULL;
		
		text = label.string;
	}
	return self;
}

- (id)initWithTexture:(NSString *)_tex {
	if(self	= [super init])
	{
		textfield = [[UITextField alloc] initWithFrame:CGRectMake(-128, -32, 10, 32)];
		[textfield addTarget:self action:@selector(changedCharacter) forControlEvents:UIControlEventEditingChanged];
		[textfield setDelegate:self];
		textfield.autocorrectionType = UITextAutocorrectionTypeNo;
		
		keyboardType = UIKeyboardTypeDefault;
		textAlignment = UITextAlignmentLeft;
		
		[[vdKernel sharedKernel].renderView addSubview:textfield];
		[textfield setHidden:YES];
		
		isKeyboardVisible = NO;
		
		background = [[vdShape alloc] initWithTexture:NULL];
		[self addChild:background];
		
		offsetLeft = 5;
		offsetRight = 5;
		
		maxCharacters = 0;
		
		label = [vdLabel labelWithText:@" "];
		label.color->red = 0.0f;
		label.color->green = 0.0f;
		label.color->blue = 0.0f;
		[background addChild:label];
		
		hideOnLostFocus = YES;
		hideOnReturn = YES;
		
		delegate = NULL;
		
		text = label.string;
		
		[self setTexture:_tex];
	}
	
	return self;
}

- (void)dealloc {
	[textfield removeFromSuperview];
	[textfield release];
	
	[[vdTexManager sharedManager] deleteTextureNode:background.texture];
	
	[self cleanChildren];
	
	[super dealloc];
}

@end
